package com.zb.uniman.controller.UnuseController;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zb.uniman.mapper.UniversityMapper;
import com.zb.uniman.common.ErrorCode;
import com.zb.uniman.common.ResultMap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Author:H
 * date:2023-11-25
 * Description:                  127.0.0.1:8080/test/hi
 * Description:                  127.0.0.1:8080/test/login
 */
@RestController
@RequestMapping("/test")
@Api(value = "学校访问中心", tags = "学校访问中心")
public class sessioncontroller {

    @ApiOperation(value = "说你好", notes = "说你好")
    @GetMapping("/hi")
    public String sayhi(HttpServletRequest request, HttpServletResponse response) {
        return "success";
    }

    @Autowired
    private UniversityMapper universityMapper;
//    @Resource
//    private MyThreadLocal threadLocal;

    /**
     * session是基于cookie实现的
     */
    /*
    postman获取cookie ，包含sessionid
     */
    @GetMapping("/login")
    public ResultMap login(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        session.setAttribute("identity","customer");
//        threadLocal.set(university);
//        return ResultMap.error(ErrorCode.sadError);
        return ResultMap.error(ErrorCode.success);
    }

    /**
     * 测试会话是否成功 -------成功
     * postman关闭后cookie失效，证明会话一次完成。-----> 证明每次登录都需要
     * 一次登录后不退出，不关闭postman，那cookie就是登录时拿到的那个，一直用到会话结束
     */
    @GetMapping("/talk")
    public ResultMap talkSession(HttpServletRequest request,HttpServletResponse response){
        HttpSession session = request.getSession();
        String identity = (String)session.getAttribute("identity");
        if (StringUtils.equals(identity,"customer")){
            System.out.println(identity);
            ResultMap<String> resultMap = new ResultMap<>();
            resultMap.setCode(200);
            resultMap.setMsg("会话成功");
            return  resultMap;
        }
        return ResultMap.error(ErrorCode.sadError);
    }
}
